import plugin from '../../../lib/plugins/plugin.js'
import common from '../../../lib/common/common.js'
import { Config } from '../components/index.js'
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const fs =require('fs')

const _path = process.cwd()
const p_path = `${_path}/plugins/js-box-plugin/resources/Research Society_map/`

const picture = JSON.parse(fs.readFileSync(`${_path}/plugins/js-box-plugin/config/data/system/map.json`, "utf8"));

let reg = new RegExp(`^#?(js-box-plugin_map帮助|${Object.keys(picture).join("|")})$`)
await initFile()
export class example extends plugin {
constructor() {
super({
name: '须弥地图',
dsc: '地图',
event: 'message',
priority: 4999,
rule: [{
reg: '^#?随机地图$',
fnc: 'xumipicture'
},
{
reg: reg,
fnc: 'appointpicture'
},
]
})
}

async xumipicture(e) {
let set = await Config.admin_set
let isplugin = set.map
if (!isplugin) return false
let file = fs.readdirSync(p_path)
let imgnum = Math.round(Math.random() * (file.length - 1))
e.reply(segment.image(p_path + file[imgnum]))
}

async appointpicture(e) {
let set = await Config.admin_set
let isplugin = set.map
if (!isplugin) return false
if (e.msg.includes('帮助')) {
e.reply(Object.keys(picture).join('\n'))
return true
}
e.reply(segment.image(`${p_path}${e.msg}.png`))
}
}

//文件初始化
async function initFile() {
if (fs.existsSync(p_path)) return
logger.info('[js-box-plugin][map]初始化资源中..')
let data = Object.keys(picture)
let url = Object.values(picture)
let fileUrl
let savePath
for (let i = 0; i < data.length; i++) {
savePath = `${p_path}${data[i]}.png`
fileUrl = url[i]
await common.downFile(fileUrl,savePath)
logger.info(`[js-box-plugin][map]资源“${data[i]}”下载成功!`)
} 
}